package cn.estore.dao;

import cn.estore.entity.CustomerEntity;
import cn.estore.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 会员表操作类，负责对该表的存取操作
 */
public class CustomerDao {
    private Connection conn = null;
    private PreparedStatement ps = null;
    private DBConnection jdbc = null;

    public CustomerDao() {
        this.jdbc = new DBConnection();
        this.conn = this.jdbc.connection;
    }

    //以会员名为条件从数据表中查询信息
    public CustomerEntity selectCustomerByName(String name) {
        CustomerEntity user = null;
        try {
            // 准备语句对象
            ps = conn.prepareStatement("select * from tb_customer where user_name = ?");
            // 设置查询参数
            ps.setString(1, name);
            // 执行查询
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                user = new CustomerEntity();
                user.setId(rs.getInt("id")); // 设置用户ID
                user.setUserName(rs.getString("user_name")); // 设置用户名称
                user.setPassword(rs.getString("password")); // 设置用户密码
                user.setRealName(rs.getString("real_name")); // 设置用户真实姓名
                user.setMobile(rs.getString("mobile")); // 设置用户手机号码
                user.setEmail(rs.getString("email")); // 设置用户电子邮件
                user.setPasswordQuestion(rs.getString("password_question")); // 设置用户密码问题
                user.setPasswordHintAnswer(rs.getString("password_hint_answer")); // 设置用户密码提示答案
            }
        } catch (SQLException e) {
            System.out.println("数据库访问失败");
        }
        return user;
    }


}
